/* eslint no-unused-vars:0 */
import React from 'react';
import PubSub from 'pubsub-js';
import './../index.less';

import { Link } from 'react-router-dom';
import { Row, Col, Icon, Input, Pagination } from 'antd';
import { Table, Button, Divider, Popconfirm, Modal } from 'antd';

import AddForm from './AddForm';

const url = 'publicSentiment';
export default class PublicSentiment extends React.Component {
    constructor(props) {
        super(props);

        this.getData(1);
    }

    state = {
        data: [],
        columns: [
            {
                title: '舆情主题',
                key: 'name',
                inputType: 'input',
                dataIndex: 'name',
                editable: true
            },
            {
                title: '操作',
                key: 'report',
                render: (text, record) => {
                    return (
                        <div>
                            <a onClick={this.jumpUrl.bind(text, record)}>
                                看具体报告
                            </a>
                        </div>
                    );
                }
            },
            {
                title: '操作',
                key: 'action',
                render: (text, record) => {
                    if (record.name == '工作简报') return '无操作';
                    else
                        return (
                            <div>
                                <a
                                    onClick={this.handleUpdate.bind(
                                        text,
                                        record
                                    )}>
                                    修改
                                </a>
                                <Divider type="vertical" />
                                <a
                                    onClick={this.handleAuthority.bind(
                                        text,
                                        record
                                    )}>
                                    板块访问授权
                                </a>
                                <Divider type="vertical" />
                                <Popconfirm
                                    title={'确定要删除这条数据?'}
                                    onConfirm={() =>
                                        this.handleDelete(text, record)
                                    }>
                                    <a>删除</a>
                                </Popconfirm>
                            </div>
                        );
                }
            }
        ],
        visible: false,
        option: {
            id: '',
            name: ''
        },
        page: 1,
        size: 5,
        total: 0
    };

    getData = (page) => {
        this.$post(url + '/all', {
            page: page,
            size: this.state.size
        })
            .then((res) => {
                if (res) {
                    var data = res.result.content;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }
                    this.setState({
                        data: data,
                        total: res.result.totalElements
                    });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    jumpUrl = (text, record) => {
        sessionStorage.setItem('showReport', JSON.stringify(text));
        this.props.history.push(
            '/admin/Backmanage/PublicOpinionReportManage/Report'
        );
    };

    handleAuthority = (text, record) => {
        sessionStorage.setItem('authority', JSON.stringify(text));
        this.props.history.push(
            '/admin/Backmanage/PublicOpinionReportManage/SentimentUser'
        );
    };

    handleAdd = (data, record) => {
        this.setState({
            visible: true
        });
    };

    handleUpdate = (data, record) => {
        this.setState({
            option: data,
            visible: true
        });
    };

    handleDelete = (data, record) => {
        console.log(data);
        this.$post(url + '/delete', data.id)
            .then((res) => {
                if (res) {
                    this.$showNotification('success', '删除成功');
                    this.getData(this.state.page);
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    handleForm = () => {
        this.setState({
            visible: false,
            option: {
                id: '',
                name: ''
            }
        });
    };

    getReturn = (data) => {
        this.getData(this.state.page);
    };

    pageChange = (page, size) => {
        this.setState({
            page: page
        });
        this.getData(page);
    };

    render() {
        return (
            <div className="body">
                <span className="opinion-body">舆情报告管理</span>
                <Row />
                <Row className="overview-row2">
                    <Button onClick={this.handleAdd}>添加</Button>
                    <Table
                        columns={this.state.columns}
                        dataSource={this.state.data}
                        pagination={{
                            current: this.state.page,
                            pageSize: this.state.size,
                            total: this.state.total,
                            onChange: this.pageChange
                        }}
                    />
                </Row>

                <AddForm
                    visible={this.state.visible}
                    data={this.state.option}
                    handleForm={this.handleForm}
                    getReturn={this.getReturn}
                />
            </div>
        );
    }
}
